from lxml import etree
import requests
import datetime
import random
import string
import time
from lxml import html
import os
import logging
import diytool
from diytool import uuid_maker
from diytool import randomStr
from send_soap_xml import post_C2
a= 1
b= 1
def write_package_xml(path):

    parser = etree.XMLParser(encoding="utf-8")
    tree = etree.parse(r"/home/projects/xmlsource/package.xml", parser=parser)

    code = 'package' + uuid_maker(25)
    programObjNode = tree.xpath('//Object[@ElementType="Package"]')[0]
    programObjNode.set("Code", code)
    programObjNode.set("ID", code)

    # 生成8位随机字符串
    #packagename = randomStr(8)
    a = random.randint(1, 1000)
    packagename = diytool.packageNamelist[int(a-1)]+randomStr(8)+'内容包'
    name = tree.xpath('//Object[@ElementType="Package"]/Property[@Name="Name"]')[0]
    name.text = packagename
    code2 = tree.xpath('//Object[@ElementType="Package"]/Property[@Name="Code2"]')[0]
    code2.text = code

    tree.write(path, pretty_print=True, xml_declaration=True, encoding='utf-8')
    print("generate package: " + packagename)
    list = [code,packagename]
    return list

def write_dtl_xml(path,list):

    parser = etree.XMLParser(encoding="utf-8")
    tree = etree.parse(r"D://xmlsource/packagedtl.xml", parser=parser)

    code = 'program' + uuid_maker(26)
    mcode = 'movie' + uuid_maker(27)
    pcode = 'picture' + uuid_maker(25)

    programObjNode = tree.xpath('//Object[@ElementType="Program"]')[0]
    programObjNode.set("Code", code)
    programObjNode.set("ID", code)

    #programname = diytool.randomStr(10)
    programname = diytool.programNamelist[int(b)]
    name = programObjNode.find('Property[@Name="Name"]')
    name.text = programname
    description = programObjNode.find('Property[@Name="Description"]')
    description.text = programname + " is an excellent film, worthy of recommendation !"

    movieObjNode = tree.xpath('//Object[@ElementType="Movie"]')[0]
    movieObjNode.set("Code", mcode)
    movieObjNode.set("ID", mcode)
    mvname = movieObjNode.find('Property[@Name="Name"]')
    mvname.text = programname

    picObjNode = tree.xpath('//Object[@ElementType="Picture"]')[0]
    picObjNode.set("Code", pcode)
    picObjNode.set("ID", pcode)

    mappingNode1 = tree.xpath('//Mapping[@ElementType="Movie"][@ParentType="Program"]')[0]
    mappingNode1.set("ParentCode", code)
    mappingNode1.set("ParentID", code)
    mappingNode1.set("ElementCode", mcode)
    mappingNode1.set("ElementID", mcode)


    mappingNode2 = tree.xpath('//Mapping[@ElementType="Program"][@ParentType="Picture"]')[0]
    mappingNode2.set("ParentCode", pcode)
    mappingNode2.set("ParentID", pcode)
    mappingNode2.set("ElementCode", code)
    mappingNode2.set("ElementID", code)


    mappingNode3 = tree.xpath('//Mapping[@ElementType="Program"][@ParentType="Package"]')[0]
    mappingNode3.set("ParentCode", list[0])
    mappingNode3.set("ParentID", list[0])
    mappingNode3.set("ElementCode", code)
    mappingNode3.set("ElementID", code)
    ElementName = mappingNode3.find('Property[@Name="ElementName"]')
    ElementName.text = programname
    ParentName = mappingNode3.find('Property[@Name="ParentName"]')
    ParentName.text = list[1]

    tree.write(path, pretty_print=True, xml_declaration=True, encoding='utf-8')
    print("generate program: " + programname +" successfully")


while True:
    a = a+1
    request_url = "http://172.19.97.161:8138/scms-injection-adapter/axis/services/ctms"
    CSPID = "10089"
    LSPID = "10089"
    #CorrelateID = diytool.timeStr()
    #path = "/opt/wacos/worksheet/package" + uuid_maker(26) + ".xml"
    #list = write_package_xml(path)
    #path = path.replace("D:", "ftp://wacos:wacos@172.19.97.207:21")
    #post_C2(request_url, CSPID, LSPID, CorrelateID, path)
    #print("waiting for " + str(5) + "s ......")
    #time.sleep(5)
    CorrelateID = diytool.timeStr()
    # path = "D:/opt/wacos/worksheet/category" + uuid_maker(26) + ".xml"
    # list = write_categroy_xml(path)
    # path = path.replace("D:", "ftp://wacos:wacos@172.20.1.20:21")
    nowdate = datetime.datetime.now().strftime('%Y%m%d')
    hour = datetime.datetime.now().strftime('%H')
    #path ="D:/opt/wacos/worksheet/"
    path = "/opt/wacos/worksheet/package/" + nowdate+ "/" + hour + "/"
    xmlfile  = path + "package" + uuid_maker(25)+".xml"
    if not os.path.exists(path):
        print("# path not exists")
        os.makedirs(path)
    list=write_package_xml(xmlfile)
   # FileURL = path.replace("D:", "ftp://wacos:wacos@172.20.1.20:21") + xmlfilename
    #warnings.filterwarnings("ignore")
    FileURL ="ftp://wacos:wacos@172.19.97.208:21/" + xmlfile
    logging.info("package_worksheet_xml ftp file path-->>" + FileURL)
    post_C2(request_url, CSPID, LSPID, CorrelateID, FileURL)
    print("waiting for " + str(5) + "s ......")
    time.sleep(5)
    orderflag = '0'
    list.append(orderflag)
    #total=5 #明细数量
    #for i in range(1,total+1):
       # path =  "/opt/wacos/worksheet/program" + uuid_maker(25) + ".xml"
        #write_dtl_xml(path,list)
        #path = path.replace("D:", "ftp://wacos:wacos@172.19.97.207:21")
        #post_C2(request_url, CSPID, CSPID, CorrelateID, path)
        #b = b +1
        #time.sleep(2)
    print("waiting for 10s... ")
    #time.sleep(10)



